package a66weding.com.jiehuntong.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.dao.db.address.AddressDao;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.widgets.wheelview.OnWheelChangedListener;
import a66weding.com.jiehuntong.widgets.wheelview.WheelView;
import a66weding.com.jiehuntong.widgets.wheelview.adapters.ArrayWheelAdapter;


/**
 * 这是设置家乡的Activity
 * Created by Administrator on 2016/7/15.
 */
public class SetHomeActivity extends CityActivity implements View.OnClickListener,OnWheelChangedListener , OnNetResultListener {
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    private TextView mCancelTv;
    private TextView mConfimTv;
    private String homeTown;

    private LinearLayout mProgressLay;

    private AddressDao addressDao;
    private List<String> mProvinceList = new ArrayList<String>();
    private List<String>  mCurrentCityList = new ArrayList<String>();
    private List<String>  mCurrentAreaList = new ArrayList<String>();

    private String [] provinceArr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cascade);
        addressDao = new AddressDao(this);
        setUpViews();
        setUpListener();
        setUpData();
    }

    private void setUpViews() {
        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);
        mCancelTv = (TextView) findViewById(R.id.tv_cancel);
        mConfimTv = (TextView) findViewById(R.id.tv_confim);
        mProgressLay = (LinearLayout) findViewById(R.id.lay_progress);
        mProgressLay.setVisibility(View.VISIBLE);
    }

    private void setUpListener() {
        // 添加change事件
        mViewProvince.addChangingListener(this);
        // 添加change事件
        mViewCity.addChangingListener(this);
        // 添加change事件
        mViewDistrict.addChangingListener(this);
        // 添加onclick事件
        mCancelTv.setOnClickListener(this);
        mConfimTv.setOnClickListener(this);
    }

    private void setUpData() {

        Set<String> provinceList = addressDao.getProvinceList();
        mProvinceList = new ArrayList<String>(provinceList);
        provinceArr = new String[mProvinceList.size()];
        provinceArr = mProvinceList.toArray(provinceArr);
        mViewProvince.setVisibleItems(7);

        mViewProvince.setViewAdapter( new ArrayWheelAdapter<String>(SetHomeActivity.this, provinceArr));

        mCurrentProviceName=mProvinceList.get(mViewProvince.getCurrentItem());

        // 设置可见条目数量
        updateCities();

        mProgressLay.setVisibility(View.GONE);
    }

    /**
     * 根据当前的市，更新区WheelView的信息
     */
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();

        mViewDistrict.setVisibleItems(7);

        Set<String> areaListByPNameCName = addressDao.getAreaListByPNameCName(mCurrentProviceName, mCurrentCityName);
        mCurrentAreaList = new ArrayList<String>(areaListByPNameCName);
        String[] areas = new String[mCurrentAreaList.size()];
        areas = mCurrentAreaList.toArray(areas);

//

        if (areas == null) {
            areas = new String[] { "" };
        }
        mViewDistrict.setViewAdapter( new ArrayWheelAdapter<String>(this, areas));
        mViewDistrict.setCurrentItem(0);
        mCurrentDistrictName = mCurrentAreaList.get(mViewDistrict.getCurrentItem());
    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    private void updateCities() {

        Set<String> cityListByPName = addressDao.getCityListByPName(mCurrentProviceName);
        mCurrentCityList = new ArrayList<String>(cityListByPName);
        String[] cities = new String[mCurrentCityList.size()];
        cities = mCurrentCityList.toArray(cities);


        if (cities == null) {
            cities = new String[] { "" };
        }
        mViewCity.setVisibleItems(7);
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        mViewCity.setCurrentItem(0);

        mCurrentCityName=mCurrentCityList.get(mViewCity.getCurrentItem());

        updateAreas();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:    //点击取消按钮,取消退出activity
                finish();
                break;
            case R.id.tv_confim:
                homeTown = mCurrentProviceName + mCurrentCityName + mCurrentDistrictName;
                String url= UrlDealUtil.getRealUrl(RequestCode.UPDATE_HOMETOWN);
                HashMap<String,String> requestParams=new HashMap<String,String>();
                requestParams.put("Hometown",""+homeTown);
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                        this, CommonResultBean.class, RequestCode.UPDATE_HOMETOWN, requestParams);
                HttpHandleUtil.executeCommonPost(this,httpRequestBean);

                break;

        }
    }


    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewProvince) {
            mCurrentProviceName = mProvinceList.get(newValue);
            updateCities();
        } else if (wheel == mViewCity) {
            mCurrentCityName=mCurrentCityList.get(newValue);
            updateAreas();
        } else if (wheel == mViewDistrict) {
            mCurrentDistrictName = mCurrentAreaList.get(newValue);
        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        //更新内存
        DataWareHourse.getInstance().getmUser().getData().get(0).setNvc_Address(homeTown);
        //更新本地缓存
        CacheUtils.saveObjectToTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, DataWareHourse.getInstance().getmUser());
        Toast.makeText(this,"设置家乡成功",Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onError(ErrorBean errorBean) {
        CUtils.showDataErrToast(this);
    }
}
